package com.wanchuang.shengfubao.module.machines;

/**
 * 文件名 : EarningDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;


import com.google.protobuf.InvalidProtocolBufferException;
import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.Mystock;
import com.wanchuang.shengfubao.utils.ValidateUtils;
import com.ruffian.library.widget.RLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllowBackActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_select_produce)
    TextView tvSelectProduce;
    @BindView(R.id.tv_select_sn_number)
    TextView tvSelectSnNumber;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.ll_select_sn_number)
    RLinearLayout llSelectSnNumber;
    @BindView(R.id.tv_goods)
    TextView tv_goods;

    private String goods_id = "";
    private String sn_up_code = "";
    private String phone;
    private String memberid;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allow_back);
        unbinder = ButterKnife.bind(this);
        tv_goods.setMovementMethod(new ScrollingMovementMethod());
        tvTitle.setText("发起回拨");
        initListener();

    }

    private void initListener() {
        backIv.setOnClickListener(v -> {
            finish();
        });
        tvSelectProduce.setOnClickListener(view -> {
            if (ValidateUtils.isValidate(memberid)) {
                Intent intent = new Intent(mContext, AllocMachineActivity.class);
                intent.putExtra("title", "选择回拨产品");
                intent.putExtra("member_id", memberid);
                intent.setType("1");
                startActivityForResult(intent, 100);
            } else {
                showToast("请先选择回拨伙伴");
            }
        });
        llSelectSnNumber.setOnClickListener(view -> {
            boolean empty = TextUtils.isEmpty(goods_id);
            if (empty) {
                showToast("请先选择回拨机器类型");
            } else {
                Intent intent = new Intent(mContext, AllowSnActivity.class);
                intent.putExtra("goods_id", goods_id);
                intent.putExtra("member_id", memberid);
                startActivityForResult(intent, 100);
            }

        });
        btnSubmit.setOnClickListener(view -> submit());
        tvName.setOnClickListener(view -> {
            Intent intent = new Intent(mContext, AllowPartnerActivity.class);
            intent.putExtra("title", "选择回拨的伙伴");
            intent.putExtra("type", "2");
            startActivityForResult(intent, 100);
        });


        Mystock.title_list.Builder builder =  Mystock.title_list.newBuilder();
        builder.setType("2");
        Mystock.title_list data = builder.build();
        NetworkManager.INSTANCE.post(Apis.title_list, data.toByteArray(),new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                Mystock.title_list result = Mystock.title_list.parseFrom(bytes);
                List<Mystock.goods> list = result.getListDesList();

                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getGoodsSerial()).append("\n");
                }
                des.append(list.get(list.size() - 1).getGoodsSerial());
                tv_goods.setText(des.toString());

            }

            @Override
            public void onError(int code, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }


    private void submit() {
        String name = tvName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请先选择回拨");
            return;
        }
        if (TextUtils.isEmpty(goods_id)) {
            showToast("请先选择回拨产品");
            return;
        }

        if (TextUtils.isEmpty(sn_up_code)) {
            showToast("请先选择SN号");
            return;
        }
        Mystock.initiateATransferNew.Builder builder = Mystock.initiateATransferNew.newBuilder();
        builder.setGoodsId(goods_id).setSnCode(sn_up_code).setMemberName(name).setMemberId(memberid)
                .setMemberMobile(phone).setNum(tvNumber.getText().toString());
        NetworkManager.INSTANCE.post(Apis.initiateATransferBack, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) {

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
                if (errorCode == 200) {
                    finish();
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 200) {
            tvName.setText(data.getStringExtra("name"));
            phone = data.getStringExtra("phone");
            memberid = data.getStringExtra("id");
            tvSelectProduce.setText("");
            tvSelectSnNumber.setText("");
            tvNumber.setText("");
            sn_up_code = "";
            goods_id = "";

        } else if (resultCode == 300) {
            tvSelectProduce.setText(data.getStringExtra("goods_name"));
            goods_id = data.getStringExtra("goods_id");
        } else if (resultCode == 400) {
            tvSelectSnNumber.setText(data.getStringExtra("sn_code"));
            sn_up_code = data.getStringExtra("sn_up_code");
            tvNumber.setText(data.getStringExtra("sn_number"));
        }
    }

}
